# coding:utf-8
import os
import sys
root = os.path.dirname(__file__) + '/..'
sys.path.append(root)

from flask import Flask, request, jsonify, g, render_template, redirect, url_for, session, current_app
from model import User, db_session, vnf
import json
from . import web
import pika
#from customConfig.msg import WebCommand

RABBITMQ_HOST = '127.0.0.1'
RABBITMQ_PORT = 5672
RABBITMQ_USERNAME = 'admin'
RABBITMQ_PASSWORD = 'admin'


@web.teardown_request
def handle_teardown_request(exception):
    db_session.remove()


@web.route('/send-command', methods=['GET', 'POST'])
def send_command():
    if request.method == 'GET':
        vnfLists = vnf.query.all()
        return render_template('web/send-command.html', vnfLists=vnfLists)
    else:
        data = request.get_json()
        command_id = data.get('command_id')
        chat_from = '13764408552'
        chat_to = data.get('chat_to')
        chat_content = data.get('content')

        if not chat_to or not chat_content or not command_id:
            return jsonify({'code': 0, 'message': '信息不完整'})

        send_data = json.dumps(dict(command_id=command_id, chat_from=chat_from, chat_to=chat_to, chat_content=chat_content))
        # current_app.redis.lpush('vnfDemo', send_data)

        credentials = pika.PlainCredentials(RABBITMQ_USERNAME, RABBITMQ_PASSWORD)
        connection = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST, credentials=credentials, port=RABBITMQ_PORT))
        channel = connection.channel()
        channel.queue_declare(queue='vnfDemo')

        channel.basic_publish(exchange='',
                              routing_key='vnfDemo',
                              body=send_data)

        print "send json_data to vnfDemo, the data is ", send_data

        connection.close()

        return jsonify({'code': 1, 'message': '发送成功'})

@web.route('/vnfInfo', methods=['POST'])
def get_vnf_info():
    if request.method == 'POST':
        data = request.get_json()
        target = data.get('target')

        send_data = json.dumps(
            dict(target=target))
        # current_app.redis.lpush('vnfDemo', send_data)

        credentials = pika.PlainCredentials(RABBITMQ_USERNAME, RABBITMQ_PASSWORD)
        connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=RABBITMQ_HOST, credentials=credentials, port=RABBITMQ_PORT))
        channel = connection.channel()
        channel.queue_declare(queue='vnfDemo')

        channel.basic_publish(exchange='',
                              routing_key='vnfDemo',
                              body=send_data)

        print "send json_data to vnfDemo, the data is ", send_data

        connection.close()

        return jsonify({'code': 1, 'message': '发送成功'})

@web.route('/downloadGame', methods=['GET', 'POST'])
def download_game():
    if request.method == 'GET':
        vnfLists = vnf.query.all()
        return render_template('web/send-command.html', vnfLists=vnfLists)
    else:
        data = request.get_json()
        command_id = data.get('command_id')
        chat_from = '13764408552'
        chat_to = data.get('chat_to')
        chat_content = data.get('content')

        if not chat_to or not chat_content or not command_id:
            return jsonify({'code': 0, 'message': '信息不完整'})

        send_data = json.dumps(dict(command_id=command_id, chat_from=chat_from, chat_to=chat_to, chat_content=chat_content))
        # current_app.redis.lpush('vnfDemo', send_data)

        credentials = pika.PlainCredentials(RABBITMQ_USERNAME, RABBITMQ_PASSWORD)
        connection = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST, credentials=credentials, port=RABBITMQ_PORT))
        channel = connection.channel()
        channel.queue_declare(queue='vnfDemo')

        channel.basic_publish(exchange='',
                              routing_key='vnfDemo',
                              body=send_data)

        print "send json_data to vnfDemo, the data is ", send_data

        connection.close()

        return jsonify({'code': 1, 'message': '发送成功'})